package com.paireach.bw.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.widget.FrameLayout;

import com.paireach.bw.R;
import com.paireach.bw.base.BaseActivity;
import com.paireach.bw.bean.OrderResult;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.fragment.AllFrag245;
import com.paireach.bw.fragment.BackFrag212;
import com.paireach.bw.fragment.BackFrag220;
import com.paireach.bw.fragment.BackFrag226;
import com.paireach.bw.fragment.BackFrag228;
import com.paireach.bw.fragment.BackFrag230;
import com.paireach.bw.fragment.BackFrag238;
import com.paireach.bw.fragment.BackFrag240;
import com.paireach.bw.fragment.ErrorFragment;
import com.paireach.bw.http.RequestTag;
import com.paireach.bw.http.WayBillRequest;
import com.paireach.bw.utils.GsonUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ${cqc} on 2017/6/19.
 * 回空没有评价
 */

public class BackActivity extends BaseActivity {

    @BindView(R.id.frameLayout)
    FrameLayout frameLayout;
    private String orderCode;//负载单号
    private FragmentManager fm;
    private boolean fromNotification;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        ButterKnife.bind(this);
        orderCode = getIntent().getStringExtra("orderCode");
        fromNotification = getIntent().getBooleanExtra("fromNotification", false);
        fm = getSupportFragmentManager();
        getOrderCodeDetail();
    }


    public void getOrderCodeDetail() {
        showProgressDialog();
        new WayBillRequest().getOrderCodeDetail(phoneNumber, orderCode);
    }


    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        dismissProgressDialog();
        if (event.getRequestTag() == RequestTag.GET_ORDER_CODE_DETAIL) {
            finish();
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        dismissProgressDialog();
        if (event.getRequestTag() == RequestTag.GET_ORDER_CODE_DETAIL) {
            if (TextUtils.isEmpty(event.getJson())) {
                showToast("没有获取到运单详情");
            } else {
                OrderResult orderResult = GsonUtil.jsonToBean(event.getJson(), OrderResult.class);
                if (orderResult.getStatus() == 1) {
                    FragmentTransaction transaction = fm.beginTransaction().setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                    String status = orderResult.getOrders().get(0).getSHPM_STATUS();
                    switch (status) {
                        case "212":
                            transaction.replace(R.id.frameLayout, BackFrag212.getInstance(orderResult,fromNotification)).commitAllowingStateLoss();
                            break;
                        case "220":
                            transaction.replace(R.id.frameLayout, BackFrag220.getInstance(orderResult)).commitAllowingStateLoss();
                            break;
                        case "226":
                            transaction.replace(R.id.frameLayout, BackFrag226.getInstance(orderResult)).commitAllowingStateLoss();
                            break;
                        case "228":
                            transaction.replace(R.id.frameLayout, BackFrag228.getInstance(orderResult)).commitAllowingStateLoss();
                            break;
                        case "230":
                            transaction.replace(R.id.frameLayout, BackFrag230.getInstance(orderResult)).commitAllowingStateLoss();
                            break;
                        case "238":
                            transaction.replace(R.id.frameLayout, BackFrag238.getInstance(orderResult)).commitAllowingStateLoss();
                            break;
                        case "240":
                            transaction.replace(R.id.frameLayout, BackFrag240.getInstance(orderResult)).commitAllowingStateLoss();
                            break;
                    }
                }
            }
        }
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        if ("GetOrderCodeDetail".equals(event.getTag())) {
            getOrderCodeDetail();
        } else if ("加载失败".equals(event.getTag())) {
            FragmentTransaction transaction = fm.beginTransaction().setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
            transaction.replace(R.id.frameLayout, ErrorFragment.getInstance(orderCode)).commitAllowingStateLoss();
        }
    }
}
